package com.geek.spring.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 * 反射：运行时自省，直接操作类、对象
 * 动态代理：运行时动态构建代理、动态处理代理方法机制。（RPC调用、面向切面编程AOP）
 * JDK 动态代理
 * https://time.geekbang.org/column/article/7489
 */
public class MyDynaProxy {

    public static void main(String[] args) {
        Hello hello = new HelloImpl();

//        MyInvocationHandler handler = new MyInvocationHandler(hello);
//        Hello proxy = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), HelloImpl.class.getInterfaces(), handler);


        Hello proxy = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), HelloImpl.class.getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("proxy：" + "此处是JDK动态代理增强的内容");
                Object object = method.invoke(hello);
                return object;
            }
        });
        proxy.sayHello();

        // java9
        List<Integer> collect = Stream.iterate(10, x -> x < 20, x -> x + 2).collect(Collectors.toList());
        System.out.println(collect);

    }
}
